package com.cryptape.cita_wallet.util;

import android.app.Activity;

import com.alm.token.R;
import com.blankj.utilcode.util.LogUtils;

import org.web3j.crypto.Keys;
import org.web3j.utils.Numeric;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class AddressUtil {

    public static boolean isAddressValid(String address) {
        if (address.length() != 40 && address.length() != 42) return false;
        address = Numeric.prependHexPrefix(address);
        String smallRegex = "^0x[a-f0-9]{40}$";
        String mixRegex = "^0x[a-fA-F0-9]{40}$";
        if (address.matches(smallRegex)) {
            return true;
        } else {
            return address.matches(mixRegex) && Keys.toChecksumAddress(address).equals(address);
        }
    }

    //读取CSV文件
    public static List<String> readCSV(Activity activity) {
        List<String> list = new ArrayList<String>();
        try {
            InputStream is = activity.getResources().openRawResource(R.raw.bulk_ether_accounts);//am.open("bulk_ether_accounts.csv");
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            try {
                String line;
                while ((line = reader.readLine()) != null) {
                    LogUtils.dTag("source-->", " line = " + line);
                    String[] RowData = line.split(",");
                    for (int i = 0; i < RowData.length; i++) {
                        String date = RowData[i];
                        LogUtils.dTag("source-->", " date = " + date);
                        list.add(date);
                    }
                }
            } catch (IOException ex) {
                // handle exception
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    // handle exception
                }
            }

        } catch (Exception e) {

        }
        return list;
    }
}
